import request from '../../utils/request'
import { uploadFileToQiniu } from '../../utils/qiniuUpload'

Page({
  data: {
    imageFiles: []
  },

  chooseImages() {
    const that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      success(res) {
        that.setData({
          imageFiles: res.tempFiles
        });
      }
    });
  },

  async onSubmit() {
    if (this.data.imageFiles.length < 1) {
      wx.showToast({
        title: '请先上传至少一张图片',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({ title: '上传中...' });

    try {
      const uploadResults = [];
      for (const file of this.data.imageFiles) {
        // 上传文件到七牛，第二个参数是你的路由目录
        const res = await uploadFileToQiniu(file.tempFilePath, 'ai-train-lora');
        uploadResults.push(res);
      }

      wx.hideLoading();
      wx.showToast({ title: '上传成功' });
      console.log('上传结果：', uploadResults);

      // 这里可以根据上传结果，继续做提交素材的业务逻辑

    } catch (error) {
      wx.hideLoading();
      wx.showToast({ title: '上传失败', icon: 'none' });
      console.error(error);
    }
  }
});
